//
//  TSTagView.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/14.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSTagView: UIView {

    var items = Array<String>()
    var tagList = Array<TSTagItem>()
    var tagHeight : CGFloat = 30
    var margin : CGFloat = 10
    var top : CGFloat = 15
    var selectedItemTitle : String!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func resetTagView() {
        
        for index in 0..<tagList.count {
            let tagItem = tagList[index]
            tagItem.itemButton.isSelected = index == 0
            if tagItem.itemButton.isSelected {
                tagItem.backgroundColor = BASE_RED_COLOR
                selectedItemTitle = tagItem.itemButton.currentTitle
            } else {
                tagItem.backgroundColor = WHITE_COLOR
            }
            
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if tagList.count == items.count && tagList.count != 0 {
            let count = items.count
            let width = self.frame.size.width
            var rowWidth : CGFloat = 0
            var isChange = true
            var last : TSTagItem? = nil
            for index in 0..<count {
                let tagItem = tagList[index]
                if tagItem.itemButton.isSelected {
                    tagItem.backgroundColor = BASE_RED_COLOR
                    selectedItemTitle = tagItem.itemButton.currentTitle
                } else {
                    tagItem.backgroundColor = WHITE_COLOR
                }
                var tagWidth = tagItem.titleWidthForTitle()
                rowWidth += tagWidth + margin
                if rowWidth > width - margin {
                    isChange = true
                    if tagWidth + margin * 2 > width {
                        tagWidth = (width - margin * 2)
                    }
                    rowWidth = tagWidth + margin
                }
                tagItem.snp.makeConstraints { (make) in
                    if isChange {
                        if last == nil {
                            make.top.equalToSuperview()
                        } else {
                            make.top.equalTo(last!.snp.bottom).offset(top)
                        }
                        make.left.equalTo(margin)
                        isChange = false
                    } else {
                        make.left.equalTo(last!.snp.right).offset(margin)
                        make.top.equalTo(last!.snp.top)
                    }
                    make.size.equalTo(CGSize(width: tagWidth, height: tagHeight))
                    
                    if index == count - 1 {
                        make.bottom.equalToSuperview()
                    }
                }
                last = tagItem
            }
        }
    }
}

extension TSTagView {
    
    func configTagView(itemsArray:Array<String>) {
        
        if itemsArray.count == items.count && items.count != 0 {
            for index in 0..<itemsArray.count {
                let tagItem = tagList[index]
                tagItem.title = itemsArray[index]
                tagItem.setupTagItem(title: itemsArray[index])
            }
            setNeedsLayout()
        } else {
            createTags(itemsArray: itemsArray)
        }
        
        items = itemsArray
    }
    
    func createTags(itemsArray:Array<String>) {
        for view in tagList {
            view.removeFromSuperview()
        }
        tagList.removeAll()
        
        for index in 0..<itemsArray.count {
            let tagItem = TSTagItem.init()
            tagItem.tag = index
            tagItem.title = itemsArray[index]
            tagItem.setupTagItem(title: itemsArray[index])
            tagItem.layer.borderColor = BASE_GRAY_COLOR.cgColor
            tagItem.layer.borderWidth = 1
            tagItem.clickBlock = { (tag, currentTitle) in
                TSLog("tag:\(tag) "+currentTitle)
                
                for item in self.tagList {
                    
                    item.itemButton.isSelected = item.tag == tag
                    if item.itemButton.isSelected {
                        item.backgroundColor = BASE_RED_COLOR
                        self.selectedItemTitle = item.itemButton.currentTitle
                    } else {
                        item.backgroundColor = WHITE_COLOR
                    }
                }
            }
            tagItem.itemButton.isSelected = index == 0
            
            addSubview(tagItem)
            tagList.append(tagItem)
        }
    }
    
}
